package com.youlu.campus.service.jinshuju;

import com.youlu.campus.entity.card.entity.VipGoods;
import com.youlu.campus.entity.jinshuju.JinInviteStatistics;
import com.youlu.campus.entity.jinshuju.JinUserInviteRecord;
import com.youlu.campus.entity.jinshuju.JinUserRank;
import com.youlu.campus.entity.jinshuju.dto.JinInviteStatisticsQueryDTO;
import com.youlu.campus.entity.jinshuju.vo.JinInviteStatisticsResponseVO;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : JinInviteStatisticsService
 * @description : [报名统计]
 * @createTime : [2023/7/21 14:12]
 */
@Service
@Slf4j
public class JinInviteStatisticsService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;

    public Page<JinInviteStatistics> queryStatisticsData(JinInviteStatisticsQueryDTO dto) {
        String activityId = dto.getActivityId();
        Query query = new Query(Criteria.where("activityId").is(activityId));
        long count = mongoTemplate.count(query, JinInviteStatistics.class);
        query.with(Sort.by("threshold").ascending());
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<JinInviteStatistics> list = mongoTemplate.find(query.with(pageRequest), JinInviteStatistics.class);
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    public JinInviteStatisticsResponseVO queryStatisticsRadio(JinInviteStatisticsQueryDTO dto) {
        JinInviteStatisticsResponseVO response = new JinInviteStatisticsResponseVO();
        String activityId = dto.getActivityId();
        long total = userParticipateActivityInfoService.getJoinedCount(activityId);
        long inviteCount = mongoTemplate.count(new Query(Criteria.where("activityId").is(activityId)), JinUserInviteRecord.class);
        long notInviteCount = total - inviteCount;
        BigDecimal bdc1 = new BigDecimal(inviteCount).multiply(new BigDecimal(100));
        BigDecimal bdc2 = new BigDecimal(notInviteCount);
        double ratio = notInviteCount == 0 ? 0 : bdc1.divide(bdc2, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
        response.setFissionRate(ratio + "%");
        response.setInviteCount(inviteCount);
        response.setCount(notInviteCount);
        response.setJoinCount(total);
        return response;
    }
}
